package com.ljw.community.controller;

import com.ljw.community.entity.*;
import com.ljw.community.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/adminController")
public class AdminController {

    @Resource
    private CommonService commonService;

    @Resource
    private UserService userService;

    @Resource
    private TopicService topicService;

    @Resource
    private AdminService adminService;

    @Resource
    private TagService tagService;

    @RequestMapping("/login")
    @ResponseBody
    ModelAndView login(Admin admin) {
        ModelAndView mv = new ModelAndView();
        //登录名密码空
        if (admin.getLoginName().isEmpty() || admin.getPassword().isEmpty()) {
            mv.addObject("errMsg", "登录名或密码不能为空");
            mv.setViewName("/admin/login");
            return mv;
        }
        String msg = commonService.login(admin.getLoginName(), admin.getPassword(), "admin");
        if (msg.isEmpty()) {
            mv.setViewName("redirect:/admin/index?loginName=" + admin.getLoginName());
        } else {
            mv.addObject("errMsg", msg);
            mv.setViewName("/admin/login");
        }
        return mv;
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/admin/login";
    }


    @RequestMapping("/loadUnverifiedUser")
    @ResponseBody
    AjaxRespInfo<List<User>> loadUnverifiedUser(int curr, int limit) {
        AjaxRespInfo<List<User>> ajaxRespInfo = new AjaxRespInfo<>();
        List<User> list = userService.loadUnverifiedUser(curr, limit);
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(list);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("暂无注册申请");
        }
        return ajaxRespInfo;
    }


    @RequestMapping("/modifyInfo")
    @ResponseBody
    AjaxRespInfo<User> modifyInfo(@RequestBody User user) {
        AjaxRespInfo<User> ajaxRespInfo = new AjaxRespInfo<>();
        int rowNum = userService.update(user);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("修改成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }

        return ajaxRespInfo;
    }

    /**
     * 为分页栏获取总数据条数
     *
     * @param query 查询条件
     * @param type  需要计数的页面类型
     * @return 该页面数据条数
     */
    @RequestMapping("/count")
    @ResponseBody
    AjaxRespInfo<Integer> count(String query, String type) {
        AjaxRespInfo<Integer> ajaxRespInfo = new AjaxRespInfo<>();
        int count = adminService.count(query, type);
        ajaxRespInfo.setData(count);
        return ajaxRespInfo;
    }

    @RequestMapping("/verifyUser")
    @ResponseBody
    AjaxRespInfo verifyUser(User user) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = userService.update(user);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("操作成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }

        return ajaxRespInfo;
    }

    @RequestMapping("/loadUserList")
    @ResponseBody
    AjaxRespInfo<List<User>> loadUserList(int curr, int limit, String query) {
        AjaxRespInfo<List<User>> ajaxRespInfo = new AjaxRespInfo<>();
        List<User> list = userService.loadUserList(curr, limit, query);
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(list);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("暂无信息");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/manageUser")
    @ResponseBody
    AjaxRespInfo manageUser(User user) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = userService.update(user);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("操作成功");
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }

        return ajaxRespInfo;
    }

    @RequestMapping("/loadTopicList")
    @ResponseBody
    AjaxRespInfo<List<TopicResp>> loadTopicList(int curr, int limit, String query) {
        AjaxRespInfo<List<TopicResp>> ajaxRespInfo = new AjaxRespInfo<>();
        List<TopicResp> list = topicService.loadTopicList(curr, limit, query);
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(list);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("暂无信息");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/loadTagList")
    @ResponseBody
    AjaxRespInfo<List<Tag>> loadTagList(int curr, int limit) {
        AjaxRespInfo<List<Tag>> ajaxRespInfo = new AjaxRespInfo<>();
        List<Tag> list = tagService.loadTagList(curr, limit);
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(list);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("暂无信息");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/manageTag")
    @ResponseBody
    AjaxRespInfo manageTag(Tag tag, String opera, String loginName) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = tagService.manageTag(tag, opera, loginName);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("操作成功");
            ajaxRespInfo.setCode(0);
        } else if (rowNum == 0) {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("已存在该板块");
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }

        return ajaxRespInfo;
    }

    @RequestMapping("/modifyPwd")
    @ResponseBody
    AjaxRespInfo modifyPwd(String oldPwd, String newPwd, String loginName) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = adminService.modifyPwd(oldPwd, newPwd, loginName);
        if (rowNum == 1) {
            ajaxRespInfo.setMsg("修改成功");
            ajaxRespInfo.setCode(0);
        } else if (rowNum == -1) {
            ajaxRespInfo.setMsg("原密码错误");
            ajaxRespInfo.setCode(1);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("系统错误");
        }

        return ajaxRespInfo;
    }

}
